package com.office.config;

import com.jfinal.config.Routes;
import com.jfinal.core.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Honsun on 2017\10\26.
 * 自动扫描注册路由
 */
public class AutoRoutes extends Routes {

    public  static Logger log = LoggerFactory. getLogger(AutoRoutes.class);

    private List<String> classPaths = new ArrayList<String>();

    /**包名**/
    private String packageName;

    public AutoRoutes(String packageName) {
        this.packageName = packageName;
    }

    public List<Class<?>> scanPackage(String packageName) throws ClassNotFoundException {
        List<Class<?>> classList=new ArrayList<Class<?>>();
        //先把包名转换为路径,首先得到项目的classpath
        String classpath = AutoRoutes.class.getResource("\\").getPath();
        //然后把我们的包名basPach转换为路径名
        packageName = packageName.replace(".", File.separator);
        //然后把classpath和basePack合并
        String searchPath = classpath + packageName;
        System.out.println(searchPath);
        doPath(new File(searchPath));
        //这个时候我们已经得到了指定包下所有的类的绝对路径了。我们现在利用这些绝对路径和java的反射机制得到他们的类对象
        for (String s : classPaths) {
            //把 D:\work\code\20170401\search-class\target\classes\com\baibin\search\a\A.class 这样的绝对路径转换为全类名com.baibin.search.a.A
            s = s.replace(classpath,"").replace("\\", ".").replace(".class", "");
            Class cls = Class.forName(s);
            classList.add(cls);
        }
        return classList;
    }

    private void doPath(File file) {
        if (file.isDirectory()) {//文件夹
            //文件夹我们就递归
            File[] files = file.listFiles();
            for (File f1 : files) {
                doPath(f1);
            }
        } else {//标准文件
            //标准文件我们就判断是否是class文件
            if (file.getName().endsWith(".class")) {
                //如果是class文件我们就放入我们的集合中。
                classPaths.add(file.getPath());
            }
        }
    }

    @SuppressWarnings("unchecked")
    public List<Class<? extends Controller>> getControllerClass() throws ClassNotFoundException {
        List<Class<?>> allClass = scanPackage(packageName);
        System.out.println(allClass.size());
        List<Class<? extends Controller>> controllerClasses = new ArrayList<Class<? extends Controller>>();
        for (Class<?> controllerClass : allClass) {
            controllerClasses.add((Class<? extends Controller>) controllerClass);
        }
        return controllerClasses;
    }

    @Override
    public void config() {
        try {
            List<Class<? extends Controller>> controllerClassList = getControllerClass();
            for (Class<? extends Controller> controllerClass : controllerClassList) {
                System.out.println(controllerClass);
                add("/" + controllerClass.getSimpleName().replace("Controller", "").toLowerCase(), controllerClass);
            }
        } catch (ClassNotFoundException e) {
            log.error("auto route exception ", e);
        }
    }

}
